package com.pureland.security.exceptions.serializers;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.pureland.security.exceptions.Auth2Exception;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @projectName: pureland_cloud
 * @className: com.pureland.security.exceptions.serializers.Auth2ExceptionSerializer
 * @description: OAuth2 异常序列化器
 * @author: tong.li
 * @createTime: 2020/11/17 16:24
 * @version: v1.0
 * @copyright: 版权所有 李彤 © 2020
 */
@Slf4j
public class Auth2ExceptionSerializer extends StdSerializer<Auth2Exception> {

    public Auth2ExceptionSerializer() {
        super(Auth2Exception.class);
    }

    @Override
    public void serialize(Auth2Exception value, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        gen.writeStringField("message", value.getMessage());
        gen.writeObjectField("messageId", value.getMessageId());
        gen.writeObjectField("timestamp", System.currentTimeMillis());
        gen.writeBooleanField("successful", false);
        gen.writeEndObject();
    }

}
